import { _decorator, Component, Sprite, UITransform } from 'cc';
import { CTRL_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM } from '../Enum';
import { EventManager } from '../RunTime/EventManager';
import { TILE_HEIGHT, TILE_WEIGHT } from '../Scripts/Tile/TileItem';
import { IEntity } from '../Levels';
import { StateMachine } from './StateMachine';
const { ccclass, property } = _decorator;


@ccclass('EntiyManager')
export class EntiyManager extends Component {

  // 移动使用
  x: number = 0
  y: number = 0
  fsm: StateMachine
  type: ENTITY_TYPE_ENUM
  private _direction: DIRECTION_ENUM
  private _state: ENTITY_STATE_ENUM

  get direction(): DIRECTION_ENUM {
    return this._direction
  }
  set direction(value: DIRECTION_ENUM) {
    this._direction = value
    this.fsm.setParam(PARAMS_NAME_ENUM.DIRECTION, DIRECTION_ORDER_ENUM[value])
  }

  get state(): ENTITY_STATE_ENUM {
    return this._state
  }
  set state(value: ENTITY_STATE_ENUM) {
    this._state = value
    this.fsm.setParam(value, true)
  }

  init(param: IEntity){
    const sprite = this.addComponent(Sprite)
    sprite.sizeMode = Sprite.SizeMode.CUSTOM
    const uiTransform = this.getComponent(UITransform)
    uiTransform.setContentSize(TILE_WEIGHT*4, TILE_HEIGHT*4)

    const {x, y, direction, state, type} = param
    this.x = x
    this.y = y
    this.type = type
    this.direction = direction
    this.state = state
  }

  isInState(state: ENTITY_STATE_ENUM){
    return this.state === state
  }




}
